package com.zzvcom.office.word.doc.util;


import com.zzvcom.office.word.doc.WFixItem;
import lombok.extern.slf4j.Slf4j;
import org.docx4j.wml.*;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Slf4j
public class TableProccessUtils {

    private static final BigInteger TABLE_MAX_SIZE = BigInteger.valueOf(10000);
    private static final BigInteger TABLE_MIN_SIZE = BigInteger.valueOf(1000);


    public  static void addBordersAutoFix(Tbl table) {
        if (table.getTblPr() == null) {
            table.setTblPr(new TblPr());
        }
        for (Object obj : table.getContent()) {
            if (obj instanceof Tr) {
                Tr tr = (Tr) obj;
                for (Object obj1 : tr.getContent()) {
                    Tc tc = (Tc) obj1;
                    setLine(tc);
                }
            }
        }
        TblWidth tblCellSpacing = table.getTblPr().getTblCellSpacing();
        if (tblCellSpacing != null) {
            tblCellSpacing.setW(BigInteger.ZERO);
        } else {
            log.info("tblCellSpacing");
        }
        // 获取其内部的TblPr属性设置属性
        table.getTblPr().setTblBorders(getBorders());
        //自动修复，就无线计算大小暂定
        table.getTblPr().getTblLayout().setType(STTblLayoutType.AUTOFIT);
        for (Object obj : table.getContent()) {
            if (obj instanceof Tr tr) {
                if(tr.getTblPrEx()==null){
                   tr.setTblPrEx(ObjectFactory.get().createCTTblPrEx());
               }
               tr.getTblPrEx().setTblBorders(getBorders());
            }
        }
    }

    /**
     * 设置边框样式
     *
     * @param table 需要设置表格边框的单元格
     */
    public  static void addBorders(Tbl table) {
        if (table.getTblPr() == null) {
            table.setTblPr(new TblPr());
        }
        final TblGrid tblGrid = table.getTblGrid();
        Map<BigInteger, WFixItem> mapSize = Collections.emptyMap();
        if (tblGrid != null) {
            int tmpTotal = 0;
            List<BigInteger> tableList = new ArrayList<>();
            for (TblGridCol tblGridCol : tblGrid.getGridCol()) {
                BigInteger w = tblGridCol.getW();
                tmpTotal = tmpTotal + w.intValue();
                tableList.add(w);
            }
            log.info("tblGrid.getGridCol() size={}", tableList.size());
            log.info("width={}", tableList);
             mapSize = Utils.fixW(tableList, TABLE_MIN_SIZE, TABLE_MAX_SIZE);
            for (TblGridCol tblGridCol : tblGrid.getGridCol()) {
                WFixItem wFixItem = mapSize.get(tblGridCol.getW());
                if (wFixItem != null) {
                    tblGridCol.setW(wFixItem.getResult().toBigInteger());
                }
            }
        }
        for (Object obj : table.getContent()) {
            if (obj instanceof Tr) {
                Tr tr = (Tr) obj;
                for (Object obj1 : tr.getContent()) {
                    Tc tc = (Tc) obj1;
                    setLine(tc);
                }
                List<BigInteger> tcWList = new ArrayList<>();
                for (Object obj1 : tr.getContent()) {
                    Tc tc = (Tc) obj1;
                    if (tc.getTcPr().getTcW() != null) {
                        tcWList.add(tc.getTcPr().getTcW().getW());
                    }
                }
                log.info("tc size={}", tcWList.size());
                log.info("tc w array={}", tcWList);


                //改变后的
                List<BigInteger> newWList = new ArrayList<>();
                for (Object obj1 : tr.getContent()) {
                    Tc tc = (Tc) obj1;
                    if (tc.getTcPr().getTcW() != null) {
                        BigInteger w = tc.getTcPr().getTcW().getW();
                        WFixItem wFixItem = mapSize.get(w);
                        if (wFixItem != null) {
                            BigInteger resultSize = wFixItem.getResult().toBigInteger();
                            tc.getTcPr().getTcW().setW(resultSize);
                            newWList.add(resultSize);
                        } else {
                            newWList.add(w);
                        }
                    }
                }
                log.info("tc size={}", newWList.size());
                log.info("tc w array={}", newWList);
            }
        }
        log.info("---------------------------");
        TblWidth tblCellSpacing = table.getTblPr().getTblCellSpacing();
        if (tblCellSpacing != null) {
            tblCellSpacing.setW(BigInteger.ZERO);
        } else {
            log.info("tblCellSpacing");
        }
        // 获取其内部的TblPr属性设置属性
        table.getTblPr().setTblBorders(getBorders());
        //自动修复，就无线计算大小暂定
        //table.getTblPr().getTblLayout().setType(STTblLayoutType.AUTOFIT);
        if (table.getTblPr().getShd() != null) {
            table.getTblPr().setShd(null);
        }
    }

    private static void setLine(Tc tc) {
        TcPr tcPr = tc.getTcPr();
        if (tcPr == null) {
            tc.setTcPr(ObjectFactory.get().createTcPr());
            tcPr = tc.getTcPr();
        }
        TblWidth tcW = tcPr.getTcW();
        if (tcW != null && tcW.getW() == null) {
            tcW = ObjectFactory.get().createTblWidth();
            tcW.setW(BigInteger.ZERO);
            tcPr.setTcW(tcW);
        }
        TcPrInner.TcBorders borders = new TcPrInner.TcBorders();
        borders.setLeft(getBorder());
        borders.setRight(getBorder());
        borders.setBottom(getBorder());
        borders.setTop(getBorder());
        borders.setInsideH(getBorder());
        borders.setInsideV(getBorder());
        tcPr.setTcBorders(borders);
    }

    private static TblBorders getBorders() {
        CTBorder border = getBorder();

        TblBorders borders = new TblBorders();
        borders.setBottom(border);
        borders.setLeft(border);
        borders.setRight(border);
        borders.setTop(border);
        borders.setInsideH(border);
        borders.setInsideV(border);
        return borders;
    }

    private static CTBorder getBorder() {
        CTBorder border = new CTBorder();
        border.setColor("auto");
        border.setSz(new BigInteger("1"));
        border.setSpace(new BigInteger("0"));
        border.setVal(STBorder.SINGLE);
        return border;
    }

}
